package com.first.module.acm.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.first.core.service.impl.BaseServiceImpl;
import com.first.module.acm.dao.UserSubDao;
import com.first.module.acm.model.UserSub;
import com.first.module.acm.service.UserSubService;

public class UserSubServiceImpl extends BaseServiceImpl<UserSub>
  implements UserSubService
{
  private UserSubDao dao;

  public UserSubServiceImpl(UserSubDao dao)
  {
    super(dao);
    this.dao = dao;
  }

  public Set<String> findAllUpUser(String userId)
  {
    List<String> list = this.dao.upUser(userId);
    Set set = new HashSet();
    for (String l : list) {
      set.add(l);
      List<String> newlist = this.dao.upUser(l);
      Set sets = new HashSet();
      for (String lon : newlist) {
        set.add(lon);
        sets.add(lon);
      }
      findUp(set, sets);
    }
    return set;
  }

  public void findUp(Set<String> setOld, Set<String> setNew)
  {
    Iterator it = setNew.iterator();
    while (it.hasNext()) {
      String userId = (String)it.next();
      List<String> newlist = this.dao.upUser(userId);
      setOld.add(userId);
      Set set = new HashSet();
      for (String lon : newlist) {
        if (!setOld.contains(lon)) {
          set.add(lon);
        }
      }
      findUp(setOld, set);
    }
  }

  public List<String> subUsers(String userId)
  {
    return this.dao.subUsers(userId);
  }

  public List<String> upUser(String userId)
  {
    return this.dao.upUser(userId);
  }
}

